package com.action;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.annotation.ExamLock;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pojo.QuestionBase;
import com.service.QuestionService;
import com.vo.ResponseBean;
import org.apache.ibatis.annotations.Param;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@CrossOrigin("*")
@RequestMapping("question")
public class QuestionAction {
    @Autowired
    private QuestionService questionService;
    @Autowired
    private RedissonClient redissonClient;

    @RequestMapping("/addOrUpdateQuestion")
    public ResponseBean addOrUpdateQuestion(@RequestParam Map map){
        QuestionBase questionBase = JSONObject.parseObject(JSON.toJSONString(map),QuestionBase.class);
        return new ResponseBean(0,0,questionService.saveOrUpdate(questionBase));
    }

    @RequestMapping("/searchQuestion")
    public ResponseBean searchQuestion(@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "10") int limit, @RequestParam Map map){
        IPage iPage = questionService.searchQuestion(page,limit,map);
        return new ResponseBean(200,iPage.getTotal(),iPage.getRecords());
    }

    /**
     * 随机查询试题库中的试题
     * @param questionBase  试题属性
     * @param num 试题数量
     * @return
     */
    @RequestMapping("/examinationQuestion")
    public ResponseBean examinationQuestion(QuestionBase questionBase , Integer num, String username){
        //随机查询试题库中的试题

        List list=questionService.examinationQuestion(num);
        if(list!=null){
            return new ResponseBean(200,0,list);
        }
        return new ResponseBean(500,0,null);
    }


}
